package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bean.Menu;
import com.example.bean.SysDict;
import com.example.service.MenuService;
import com.example.service.SysDictService;
import com.example.until.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @title: MenuController
 * @Author wjf
 * @Date: 2022/9/26 18:57
 * @Version 1.0
 */
@RestController
@CrossOrigin
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private SysDictService dictService;

    @GetMapping("/icon")
    public Result getIcon(){
        QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type","icon");
        List<SysDict> list = dictService.list(queryWrapper);
        return Result.ok(list);
    }

    @GetMapping("/page")
    public Result getPage(
            @RequestParam Integer current,
            @RequestParam Integer pageSize,
            @RequestParam String name) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name)
                .orderByDesc("id");
        IPage<Menu> page = new Page<>(current, pageSize);
        IPage<Menu> page1 = menuService.page(page, queryWrapper);
        System.out.println(page1);
        return Result.ok(page1);
    }

    @GetMapping
    public Result findAll(@RequestParam(defaultValue = "") String name) {
        List<Menu> menu = menuService.getMenu(name);
        return Result.ok(menu);
    }

    @GetMapping("/{id}")
    public Result<Menu> getUserById(@PathVariable("id") Integer id) {
        Menu menu = menuService.getById(id);
        return Result.ok(menu);
    }

    @GetMapping("/ids")
    public Result getAllMenus(){
       List<Integer> list= menuService.getAllMenuIds();
       return Result.ok(list);
    }

    @PostMapping("/save")
    public Result<Boolean> saveUser(@RequestBody() Menu menu) {
        boolean save = menuService.save(menu);
        return Result.ok(save);
    }

    @PutMapping("/update")
    public Result<Boolean> putUser(@RequestBody Menu menu) {
        boolean b = menuService.updateById(menu);
        return Result.ok(b);
    }

    @DeleteMapping("/delete/{id}")
    public Result<Boolean> deleteUser(@PathVariable Integer id) {
        boolean b = menuService.removeById(id);
        return Result.ok(b);
    }

    @DeleteMapping("/delete/batch/{ids}")
    public Result<Boolean> deleteMoreUser(@PathVariable List<Integer> ids) {
        boolean b = menuService.removeByIds(ids);
        return Result.ok(b);
    }
}
